Imports System.Diagnostics.Process
Imports System.Threading
Public Class F_Refund
    Dim WithEvents SBO_Application As SAPbouiCOM.Application
    Dim Ocompany As SAPbobsCOM.Company
    Sub New(ByVal ocompany1 As SAPbobsCOM.Company, ByVal sbo_application1 As SAPbouiCOM.Application)
        SBO_Application = sbo_application1
        Ocompany = ocompany1
    End Sub

    Public Sub Refund_Bind(ByVal oform As SAPbouiCOM.Form)
        Try
            CFL_BP_Customer(oform, SBO_Application)

            CFL_ACCOUNT(oform, SBO_Application)
            oform.DataSources.UserDataSources.Add("oedit1", SAPbouiCOM.BoDataType.dt_SHORT_TEXT)
            oform.DataSources.UserDataSources.Add("oedit2", SAPbouiCOM.BoDataType.dt_SHORT_TEXT)
            oform.DataSources.UserDataSources.Add("oedit4", SAPbouiCOM.BoDataType.dt_DATE)
            oform.DataSources.UserDataSources.Add("oedit5", SAPbouiCOM.BoDataType.dt_SHORT_TEXT)
            oform.DataSources.UserDataSources.Add("oedit6", SAPbouiCOM.BoDataType.dt_SHORT_TEXT)
            oform.DataSources.UserDataSources.Add("oedit7", SAPbouiCOM.BoDataType.dt_SHORT_TEXT)
            oform.DataSources.UserDataSources.Add("oedit9", SAPbouiCOM.BoDataType.dt_PRICE)
            oEdit = oform.Items.Item("4").Specific
            oEdit.DataBind.SetBound(True, "", "oedit1")
            oEdit.ChooseFromListUID = "CFLBPC"
            oEdit.ChooseFromListAlias = "CardCode"
            oEdit = oform.Items.Item("8").Specific
            oEdit.DataBind.SetBound(True, "", "oedit2")
            oEdit = oform.Items.Item("10").Specific
            oEdit.DataBind.SetBound(True, "", "oedit4")
            oEdit.String = Format(Now.Date, "dd/MM/yy")
            oEdit = oform.Items.Item("11").Specific
            oEdit.DataBind.SetBound(True, "", "oedit9")
            oCombo = oform.Items.Item("13").Specific
            oCombo.DataBind.SetBound(True, "", "oedit5")
            oCombo.ValidValues.Add("CREDIT CARD", "CC")
            oCombo.ValidValues.Add("CHEQUE", "CH")
            oCombo.ValidValues.Add("CASH", "CA")
            oCombo.ValidValues.Add("BANK TRANSFER", "BT")
            oCombo.Select("CH", SAPbouiCOM.BoSearchKey.psk_ByDescription)
            oEdit = oform.Items.Item("15").Specific
            oEdit.DataBind.SetBound(True, "", "oedit6")
            oEdit.ChooseFromListUID = "CFLACC"
            oEdit.ChooseFromListAlias = "AcctCode"
            oEdit = oform.Items.Item("18").Specific
            oEdit.DataBind.SetBound(True, "", "oedit7")

        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
        End Try

    End Sub

    Private Sub SBO_Application_ItemEvent(ByVal FormUID As String, ByRef pVal As SAPbouiCOM.ItemEvent, ByRef BubbleEvent As Boolean) Handles SBO_Application.ItemEvent
        Try


            If pVal.FormUID = "CreditNote" Then
                oForm = SBO_Application.Forms.Item("CreditNote")
                If pVal.InnerEvent = False And pVal.ItemUID = "Print" And pVal.Before_Action = False And pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED Then
                    oEdit = oForm.Items.Item("4").Specific
                    If oEdit.String = "" Then
                        SBO_Application.StatusBar.SetText("Customer Code Can't Be Empty!", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                        BubbleEvent = False
                        Exit Sub
                    End If
                    oEdit = oForm.Items.Item("11").Specific
                    If oEdit.Value = 0.0 Then
                        SBO_Application.StatusBar.SetText("Amount Can't Be Empty!", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                        BubbleEvent = False
                        Exit Sub
                    End If
                    oEdit = oForm.Items.Item("15").Specific
                    If oEdit.String = "" Then
                        SBO_Application.StatusBar.SetText("Account Code Can't Be Empty!", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                        BubbleEvent = False
                        Exit Sub
                    End If
                    oEdit = oForm.Items.Item("10").Specific
                    If oEdit.String = "" Then
                        SBO_Application.StatusBar.SetText("Posting Date Can't Be Empty!", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                        BubbleEvent = False
                        Exit Sub
                    End If
                    Dim k As Integer = 0
                    Try

                        Dim oJV As SAPbobsCOM.JournalEntries
                        oJV = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oJournalEntries)

                        oJV.TaxDate = Now.Date
                        oJV.DueDate = Now.Date
                        oEdit = oForm.Items.Item("15").Specific
                        oJV.Lines.AccountCode = oEdit.Value
                        oEdit = oForm.Items.Item("11").Specific
                        oJV.Lines.Credit = oEdit.Value
                        oJV.Lines.Add()
                        oEdit = oForm.Items.Item("4").Specific
                        oJV.Lines.ShortName = oEdit.String
                        oEdit = oForm.Items.Item("11").Specific
                        oJV.Lines.Debit = oEdit.Value
                        oJV.Lines.Add()
                        oEdit = oForm.Items.Item("18").Specific
                        oJV.Memo = oEdit.Value

                        k = oJV.Add()

                    Catch ex As Exception

                    End Try
                    oEdit = oForm.Items.Item("4").Specific
                    oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    oRecordSet.DoQuery("update ocrd set [U_AI_NoDayPass] ='' , [U_AI_NoDayPassM] ='' , [U_AI_NoDayPassB] =''  where CardCode='" & oEdit.String & "'")
                    If k = 0 Then
                        SBO_Application.MessageBox("JE Raised Successfully!")
                        oEdit = oForm.Items.Item("4").Specific
                        oEdit.String = ""
                        oEdit = oForm.Items.Item("8").Specific
                        oEdit.String = ""
                        oEdit = oForm.Items.Item("10").Specific
                        oEdit.String = ""
                        oEdit = oForm.Items.Item("11").Specific
                        oEdit.String = "0.0"
                        oEdit = oForm.Items.Item("15").Specific
                        oEdit.String = ""
                        oEdit = oForm.Items.Item("18").Specific
                        oEdit.String = ""
                    End If

                End If
                If pVal.EventType = SAPbouiCOM.BoEventTypes.et_CHOOSE_FROM_LIST Then
                    Dim oCFLEvento As SAPbouiCOM.IChooseFromListEvent
                    oCFLEvento = pVal
                    Dim sCFL_ID As String
                    sCFL_ID = oCFLEvento.ChooseFromListUID
                    oForm = SBO_Application.Forms.Item(FormUID)
                    Dim oCFL As SAPbouiCOM.ChooseFromList
                    oCFL = oForm.ChooseFromLists.Item(sCFL_ID)
                    Try
                        If oCFLEvento.BeforeAction = False Then
                            Dim oDataTable As SAPbouiCOM.DataTable
                            oDataTable = oCFLEvento.SelectedObjects
                            If pVal.ItemUID = "4" Then
                                Try
                                    oEdit = oForm.Items.Item("8").Specific
                                    oEdit.String = oDataTable.GetValue("CardName", 0)
                                    oEdit = oForm.Items.Item("4").Specific
                                    oEdit.String = oDataTable.GetValue("CardCode", 0)
                                Catch ex As Exception
                                End Try
                            End If
                            If pVal.ItemUID = "15" Then
                                Try
                                    oEdit = oForm.Items.Item("15").Specific
                                    oEdit.String = oDataTable.GetValue("AcctCode", 0)
                                Catch ex As Exception
                                End Try
                            End If

                        End If
                    Catch ex As Exception
                        '' SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                    End Try
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub
End Class
